#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
#include "wall.h"
#include "texture.h"
#include "colors.h"
#include "material.h"

/* classify a point respect to an hyperplane (>=0 the point is above the plane, <0 the point is below the plane */
#define getSign(h,p) ((h)[0]*(p)[0]+(h)[1]*(p)[1]+(h)[2])

/**************************/
/*Stanza 1*/
float stanza1_start[2] = {
45.0,
80.0
};
float stanza1_end[2] = {
85.0,
40.0
};

float S1N[]={
		5, 17.5, -4, 18.5, WALL_STRIPE,
		2, 30, -5, 5, WALL_STRIPE,
		3, 40, WALL_STRIPE,
		WALL_END	
	};
float S1S[]={
		5, 18.5, -4, 17.5, WALL_STRIPE,
		2, 30, -5, 5,WALL_STRIPE,
		3, 40, WALL_STRIPE,
		WALL_END	
	};
float S1E[]={	
		5, 17.5, -4, 18.5, WALL_STRIPE,
		2, 10, -5, 25, WALL_STRIPE,
		3, 40, WALL_STRIPE,
		WALL_END	
	};
float S1W[]={	
		5, 18.5, -4, 17.5, WALL_STRIPE,
		2, 5, -2, 23, -5, 5, WALL_STRIPE,
		3, 40, WALL_STRIPE,
		WALL_END	
	};


/*Stanza 2*/
float stanza2_start[2] = {
105.0,
75.0
};
float stanza2_end[2] = {
120.0,
45.0
};

float S2N[]={	
//		10, 15, WALL_STRIPE,
//		10, 5,-5,5, WALL_STRIPE,	//lascio una backdoor
		10, 15, WALL_STRIPE,
		WALL_END
	};
float S2E[]={	
		5, 30, WALL_STRIPE,
		2, 10, -10,10, WALL_STRIPE,
		3, 30, WALL_STRIPE,
		WALL_END	
	};
float S2S[]={	
		10, 15, WALL_STRIPE,
		WALL_END	
	};
float S2W[]={	
		5, 13.5,-4,12.5, WALL_STRIPE,
		5, 30, WALL_STRIPE,
		WALL_END	
	};

/*Stanza 3*/
float stanza3_start[2] = {
50.0,
115.0
};
float stanza3_end[2] = {
80.0,
100.0
};

float S3N[]={	
		10, 30, WALL_STRIPE,
		WALL_END	
	};
float S3W[]={	
		10, 15, WALL_STRIPE,
		WALL_END	
	};
float S3E[]={	
		10, 15, WALL_STRIPE,
		WALL_END	
	};
float S3S[]={	
		5, 13.5,-4,12.5, WALL_STRIPE,
		5, 30, WALL_STRIPE,
		WALL_END	
	};

/*Stanza 4*/
float stanza4_start[2] = {
10.0,
75.0
};
float stanza4_end[2] = {
25.0,
45.0
};

float S4N[]={	
		10, 15, WALL_STRIPE,
		WALL_END	
	};
float S4E[]={	
		5, 12.5, -4, 13.5, WALL_STRIPE,
		5, 30, WALL_STRIPE,
		WALL_END	
	};
float S4S[]={	
		10, 15, WALL_STRIPE,
		WALL_END	
	};
float S4W[]={	
		5, 30, WALL_STRIPE,
		2, 10, -10,10, WALL_STRIPE,
		3, 30, WALL_STRIPE,
		WALL_END	
	};


/*Stanza 5*/
float stanza5_start[2] = {
50.0,
20.0
};
float stanza5_end[2] = {
80.0,
5.0
};

float S5N[]={	
		5, 12.5,-4,13.5, WALL_STRIPE,
		5, 30, WALL_STRIPE,
		WALL_END	
	};
float S5E[]={	
		10, 15, WALL_STRIPE,
		WALL_END	
	};
float S5S[]={	
		10, 30, WALL_STRIPE,
		WALL_END	
	};
float S5W[]={	
		10, 15, WALL_STRIPE,
		WALL_END	
	};

////////// CORRIDOI ///////////////////////

/*Corridoio 1*/
float corridoio11_start[2] = {
85.0,
65.0
};
float corridoio11_end[2] = {
105.0,
55.0
};

float C1N[]={	
		0.5, 18, WALL_STRIPE,
		2.5,10,-2,6, WALL_STRIPE,
		7,18, WALL_STRIPE,
		WALL_END	
	};
float C1S[]={	
		10, 18, WALL_STRIPE,
		WALL_END
	};
	
	
/*Corridoio 2*/
float corridoio12_start[2] = {
60.0,
100.0
};
float corridoio12_end[2] = {
70.0,
80.0
};
float C2N[]={	
		10, 18, WALL_STRIPE,
		WALL_END	
	};
float C2S[]={	
		0.5, 18, WALL_STRIPE,
		2.5,10,-2,6, WALL_STRIPE,
		7,18, WALL_STRIPE,
		WALL_END	
	};
	
/*Corridoio 3*/
float corridoio13_start[2] = {
25.0,
65.0
};
float corridoio13_end[2] = {
45.0,
55.0
};

float C3N[]={	
		10, 18, WALL_STRIPE,
		WALL_END	
	};
float C3S[]={	
		10, 18, WALL_STRIPE,
		WALL_END	
	};
/*Corridoio 4*/
float corridoio14_start[2] = {
60.0,
40.0
};
float corridoio14_end[2] = {
70.0,
20.0
};

float C4N[]={	
		10, 18, WALL_STRIPE,
		WALL_END	
	};
float C4S[]={	
		10, 18, WALL_STRIPE,
		WALL_END
	};
	
////////	CANNONI	/////////////////////
/*cannone Nord Ovest*/
float CNO[] = {1, 4, WALL_STRIPE, WALL_END};
/*cannone Nord Est*/
float CNE[] = {1, 4, WALL_STRIPE, WALL_END};
/*cannone Sud Ovest*/
float CSO[] = {1, 4, WALL_STRIPE, WALL_END};
/*cannone Sud Est*/
float CSE[] = {1, 4, WALL_STRIPE, WALL_END};

///////		PUBBLICITA'&FIGURE	////////////////
float cocacola[] = {1, 5, WALL_STRIPE, WALL_END};;
float torretta[] = {1, 5, WALL_STRIPE, WALL_END};

float samurai[] = {1, 4, WALL_STRIPE, WALL_END};
//float samurai2[] = {1, 2, WALL_STRIPE, WALL_END};
//float samurai3[] = {1, 2, WALL_STRIPE, WALL_END};
//float samurai4[] = {1, 2, WALL_STRIPE, WALL_END};

float pilastroCentrale[] = {1, 2, WALL_STRIPE, WALL_END};
//pannello stanza 3 frontale=pannello nord nella stanza nord
float pannelloS3N[] = {1, 2, WALL_STRIPE, WALL_END};
//pannello ovest stanza nord
float pannelloS3O[] = {1, 2, WALL_STRIPE, WALL_END};

float panca[] = {1, 4, WALL_STRIPE, WALL_END};
float samurai2[] = {1, 2, WALL_STRIPE, WALL_END};

float pannello[] = {1, 5, WALL_STRIPE, WALL_END};

/**************************/

/////::::::	MURO TOTALE	::::::::////////////////////////////
struct wall_t walls[NUMBER_OF_WALLS] = 
{

/**	Stanza 1 **/
	{40, {45,80},{1,0},S1N,1.0f},		//N
	{40, {85,80}, {0,-1},S1E,1.0f},	//E
	{40, {85,40},{-1,0},S1S,1.0f},		//S
	{40, {45,40},{0,1},S1W,1.0f},	//W

/*Corridoio 1*/
	{18, {86,65},{1,0},C1N,1.0f},		//N
	{18, {86,55},{1,0},C1S,1.0f},		//S


/**	Stanza 2 	**/
	{15, {105,75},{1,0},S2N,1.0f},		//N
	{30, {120,75}, {0,-1},S2E,1.0f},	//E
	{15, {120,45},{-1,0},S2S,1.0f},		//S
	{30, {105,45},{0,1},S2W,1.0f},	//W


/*Corridoio 2*/
	{18, {70,81},{0,1},C2N,1.0f},		//N
	{18, {60,81},{0,1},C2S,1.0f},		//S

/**	Stanza 3	**/
	{30, {50,115},{1,0},S3N,1.0f},		//N
	{15, {80,115}, {0,-1},S3E,1.0f},	//E
	{30, {80,100},{-1,0},S3S,1.0f},		//S
	{15, {50,100},{0,1},S3W,1.0f},	//W

/*Corridoio 3*/
	{18, {26,65},{1,0},C3N,1.0f},		//N
	{18, {26,55},{1,0},C3S,1.0f},		//S


/**	Stanza 4 	**/
	{15, {10,75},{1,0},S4N,1.0f},		//N
	{30, {25,75}, {0,-1},S4E,1.0f},	//E
	{15, {25,45},{-1,0},S4S,1.0f},		//S
	{30, {10,45},{0,1},S4W,1.0f},	//W

/*Corridoio 4*/
	{18, {70,21},{0,1},C4N,1.0f},		//N
	{18, {60,21},{0,1},C4S,1.0f},		//S

/**	Stanza 5	**/
	{30, {50,20},{1,0},S5N,1.0f},		//N
	{15, {80,20}, {0,-1},S5E,1.0f},	//E
	{30, {80,5},{-1,0},S5S,1.0f},		//S
	{15, {50,5},{0,1},S5W,1.0f},	//W

///////////////		CANNONI	/////////////////////

/**	Cannone Nort Ovest	*/
	{4, {45,76}, {1,0}, CNO, 4.0f},
	//{2, {48,77}, {0,1}, CNO, 2.0f},
	
/**	Cannone Nord Est	*/
	{4, {81,76}, {1,0}, CSO, 4.0f},
	//{2, {84,77}, {0,1}, CSO, 2.0f},
	
/**	Cannone Sud Ovest	*/
	{4, {45,40}, {1,0}, CSO, 4.0f},
	//{2, {48,41}, {0,1}, CSO, 2.0f},
	
/**	Cannone Sud Est	*/
	{4, {81,40}, {1,0}, CSO, 4.0f},
	//{2, {84,41}, {0,1}, CSO, 2.0f},
	
/**	Pubblicita'&figure	*/
	//cocacola nellastanza S4
	{4, {20,71}, {1,0}, cocacola, 4.0f},
	{4, {10, 45}, {1,0}, cocacola, 4.0f},
	//{4, {25,71}, {0,1}, cocacola, 4.0f},
	//samurai
	{4, {58, 53}, {1,0}, samurai, 4.0f},
	{4, {68, 53}, {1,0}, samurai, 4.0f},
	{4, {58, 63}, {1,0}, samurai, 4.0f},
	{4, {68, 63}, {1,0}, samurai, 4.0f},	//38-esimo muro
	
	{3, {64, 59}, {1,0}, pilastroCentrale, 3.0f},
	
	{3, {71, 59}, {1,0}, pilastroCentrale, 3.0f},
	{3, {58, 59}, {1,0}, pilastroCentrale, 3.0f},
	{3, {64, 65}, {1,0}, pilastroCentrale, 3.0f},
	{3, {64, 53}, {1,0}, pilastroCentrale, 3.0f},
	
	{4, {29, 63}, {1,0}, panca, 2.0f},
	{4, {62, 28}, {0,1}, panca, 2.0f},
	
	//torrette nella stanza S5
	{5, {55,5}, {0,1}, torretta, 5.0f},//sud ovest
	//{2, {51,6}, {1,0}, torretta, 2.0f},

	{5, {55,15}, {0,1}, torretta, 5.0f},//nord ovest
	//{2, {51,17}, {1,0}, torretta, 2.0f},

	{5, {80, 5}, {0,1}, torretta, 5.0f},//sud est
	//{2, {77, 7}, {1,0}, torretta, 2.0f},

	{5, {80,15}, {0,1}, torretta, 5.0f},//nord est	//43
	//{2, {77,16}, {1,0}, torretta, 2.0f},
	{5, {62,113}, {1,0}, pannello, 1.0f},
	{5, {51.5,104.5}, {0,1}, pannello, 1.0f},
};

struct wall_t walls1[NUMBER_OF_WALLS_S1] = 
{

/**	Stanza 1 **/
	{40, {45,80},{1,0},S1N,1.0f},		//N
	{40, {85,80}, {0,-1},S1E,1.0f},	//E
	{40, {85,40},{-1,0},S1S,1.0f},		//S
	{40, {45,40},{0,1},S1W,1.0f},	//W

};

struct wall_t wallsc1[NUMBER_OF_WALLS_C1] = 
{
/*Corridoio 1*/
	{18, {86,65},{1,0},C1N,1.0f},		//N
	{18, {86,55},{1,0},C1S,1.0f},		//S
};

struct wall_t walls2[NUMBER_OF_WALLS_S2] = 
{
/**	Stanza 2 	**/
	{15, {105,75},{1,0},S2N,1.0f},		//N
	{30, {120,75}, {0,-1},S2E,1.0f},	//E
	{15, {120,45},{-1,0},S2S,1.0f},		//S
	{30, {105,45},{0,1},S2W,1.0f},		//W

};

struct wall_t wallsc2[NUMBER_OF_WALLS_C2] = 
{
/*Corridoio 2*/
	{18, {70,81},{0,1},C2N,1.0f},		//N
	{18, {60,81},{0,1},C2S,1.0f},		//S
};

struct wall_t walls3[NUMBER_OF_WALLS_S3] = 
{
/**	Stanza 3	**/
	{30, {50,115},{1,0},S3N,1.0f},		//N
	{15, {80,115}, {0,-1},S3E,1.0f},	//E
	{30, {80,100},{-1,0},S3S,1.0f},		//S
	{15, {50,100},{0,1},S3W,1.0f},		//W
};

struct wall_t wallsc3[NUMBER_OF_WALLS_C3] = 
{
/*Corridoio 3*/
	{18, {26,65},{1,0},C3N,1.0f},		//N
	{18, {26,55},{1,0},C3S,1.0f},		//S
};

struct wall_t walls4[NUMBER_OF_WALLS_S4] = 
{
/**	Stanza 4 	**/
	{15, {10,75},{1,0},S4N,1.0f},		//N
	{30, {25,75}, {0,-1},S4E,1.0f},	//E
	{15, {25,45},{-1,0},S4S,1.0f},		//S
	{30, {10,45},{0,1},S4W,1.0f},	//W

};

struct wall_t wallsc4[NUMBER_OF_WALLS_C4] = 
{
/*Corridoio 4*/
	{18, {70,21},{0,1},C4N,1.0f},		//N
	{18, {60,21},{0,1},C4S,1.0f},		//S
};

struct wall_t walls5[NUMBER_OF_WALLS_S5] = 
{
/**	Stanza 5	**/
	{30, {50,20},{1,0},S5N,1.0f},		//N
	{15, {80,20}, {0,-1},S5E,1.0f},	//E
	{30, {80,5},{-1,0},S5S,1.0f},		//S
	{15, {50,5},{0,1},S5W,1.0f},	//W
};


/* find the equation of a plane in 2d from 2 points */
void findPlane(float dest[3],float p0[2],float p1[2])
{
	float x0=p0[0],y0=p0[1];
	float x1=p1[0],y1=p1[1];
	dest[0]=y1-y0;
	dest[1]=x0-x1;
	dest[2]=-x0*y1+x1*y0;
}

void checkMovement(float Start[2],float End[2], struct wall_t* last_crossed_wall){
	int i;
		for (i=0;i<NUMBER_OF_WALLS;i++)
		{
			if (acrossWall(&walls[i],Start,End, last_crossed_wall))
			{
				glutPostRedisplay();
				return;
			}
		}
}

int acrossWall(struct wall_t* wall,float Start[2],float End[2], struct wall_t* last_crossed_wall)
{
	int   I=0;
	float D;
	float dimD;
	float p0[2],p1[2],p0w[2],p1w[2];
	float h0[3],h1[3],h0w[3],h0s[3],h0e[3];
	float wsize;
	//float dirp[2];
	float	dirp[2]={-1*wall->dir[1],wall->dir[0]};
	/* test intersection only for the first row of the wall (the one which is at z=0) */
	I++;

	last_crossed_wall=0; 
	
	for (D=0;dimD=wall->w[I++];D+=(float)fabs(dimD))
	{
		if (dimD<0) continue;
		wsize = wall->size;
		p0[0]=wall->point[0]+D*wall->dir[0];
		p0[1]=wall->point[1]+D*wall->dir[1];

		p1[0]=p0[0]+(dimD)*wall->dir[0];
		p1[1]=p0[1]+(dimD)*wall->dir[1];

		findPlane(h0,p0,p1);
		findPlane(h1,Start,End);
		p0w[0]=p0[0]+(dirp[0]*wsize);
		p0w[1]=p0[1]+(dirp[1]*wsize);

		p1w[0]=p1[0]+(dirp[0]*wsize);
		p1w[1]=p1[1]+(dirp[1]*wsize);

		findPlane(h0w,p0w,p1w);
		findPlane(h0s,p0w,p0);
		findPlane(h0e,p1w,p1);

		if (
(			 //test left	
			((getSign(h0e,Start)>=0 && getSign(h0e,End)<0 ) ||
				 (getSign(h0e,Start)< 0 && getSign(h0e,End)>=0)) 
			 &&
				((getSign(h1,p1w   )>=0 && getSign(h1,p1 )<0 ) ||
				 (getSign(h1,p1w  )< 0 && getSign(h1,p1)>=0))
)
|| 
(			 //test right	
			((getSign(h0s,Start)>=0 && getSign(h0s,End)<0 ) ||
				 (getSign(h0s,Start)< 0 && getSign(h0s,End)>=0)) 
			 &&
				((getSign(h1,p0w   )>=0 && getSign(h1,p0 )<0 ) ||
				 (getSign(h1,p0w  )< 0 && getSign(h1,p0)>=0))
)
|| (				((getSign(h0w,Start)>=0 && getSign(h0w,End)<0 ) ||
				 (getSign(h0w,Start)< 0 && getSign(h0w,End)>=0)) 
			 &&
				((getSign(h1,p0w   )>=0 && getSign(h1,p1w )<0 ) ||
				 (getSign(h1,p0w  )< 0 && getSign(h1,p1w )>=0))
)
||
(
				((getSign(h0,Start)>=0 && getSign(h0,End)<0 ) ||
				 (getSign(h0,Start)< 0 && getSign(h0,End)>=0)) 
			 &&
				((getSign(h1,p0   )>=0 && getSign(h1,p1 )<0 ) ||
				 (getSign(h1,p0   )< 0 && getSign(h1,p1 )>=0))
)
			)
		{
			last_crossed_wall=wall; /* to highligh a wall when it is crossed */
			return 1;
		}
	}

	return 0;
}



void drawWall(struct wall_t* wall){
	int   I=0, dimTot /*dimensione totale del muro*/;
	float D,Z/*,K*/;
	float dimD,dimZ, /*dimK,*/U0,U1,V0,V1;
	float p0[2],p1[2],p2[2],p3[2];
	float Su,Sv;
	float dirp[2]={-1*wall->dir[1],wall->dir[0]};
	vettore3d norm,v1,v2;
	punto3d pt1,pt2,pt3,pt0,pt1b,pt2b,pt3b,pt0b;
	I++;
	dimTot=wall->dimTot;
	I=0;
	for (Z=0;dimZ=wall->w[I++];Z+=dimZ)	{
		//con Z scorro il muro in altezza
		for (D=0;dimD=wall->w[I++];D+=(float)fabs(dimD)){
			//con D scorro il muro in lunghezza
			if (dimD<0) continue;
			Su = dimTot/3;
			Sv = 3;
			U0 = Su*D/dimTot;
			U1 = Su*(D+dimD)/dimTot;
			V0 = Sv*Z/10;
			V1 = Sv*(Z+dimZ)/10;
			p0[0]=wall->point[0]+D*wall->dir[0];
			p0[1]=wall->point[1]+D*wall->dir[1];

			p1[0]=p0[0]+(dimD)*wall->dir[0];
			p1[1]=p0[1]+(dimD)*wall->dir[1];

			p2[0]=p1[0]+(wall->size)*dirp[0];
			p2[1]=p1[1]+(wall->size)*dirp[1];

			p3[0]=p0[0]+(wall->size)*dirp[0];
			p3[1]=p0[1]+(wall->size)*dirp[1];
//sotto
			glVertex3f(p0[0],p0[1],Z);
			glVertex3f(p1[0],p1[1],Z);
			glVertex3f(p2[0],p2[1],Z);
			glVertex3f(p3[0],p3[1],Z);
//sopra
			glVertex3f(p0[0],p0[1],Z+dimZ);
			glVertex3f(p1[0],p1[1],Z+dimZ);
			glVertex3f(p2[0],p2[1],Z+dimZ);
			glVertex3f(p3[0],p3[1],Z+dimZ);
//front
			glNormal3f(dirp[0],dirp[1],0.0);
			glTexCoord2f(U0,V0);
			glVertex3f(p0[0],p0[1],Z     );
			glTexCoord2f(U1,V0);
			glVertex3f(p1[0],p1[1],Z     );
			glTexCoord2f(U1,V1);
			glVertex3f(p1[0],p1[1],Z+dimZ);
			glTexCoord2f(U0,V1);
			glVertex3f(p0[0],p0[1],Z+dimZ);
//right
			glNormal3f(-1.0*wall->dir[0],-1.0*wall->dir[1],0.0);
			glVertex3f(p1[0],p1[1],Z     );
			glVertex3f(p2[0],p2[1],Z     );
			glVertex3f(p2[0],p2[1],Z+dimZ);
			glVertex3f(p1[0],p1[1],Z+dimZ);
//back
			glNormal3f(-1.0*dirp[0],-1.0*dirp[1],0.0);
			glTexCoord2f(U0,V0);
			glVertex3f(p2[0],p2[1],Z	);
			glTexCoord2f(U1,V0);
			glVertex3f(p3[0],p3[1],Z	);
			glTexCoord2f(U1,V1);
			glVertex3f(p3[0],p3[1],Z+dimZ);
			glTexCoord2f(U0,V1);
			glVertex3f(p2[0],p2[1],Z+dimZ);
//left
			glNormal3f(1.0*wall->dir[0],1.0*wall->dir[1],0.0);
			glVertex3f(p3[0],p3[1],Z	);
			glVertex3f(p0[0],p0[1],Z	);
			glVertex3f(p0[0],p0[1],Z+dimZ);
			glVertex3f(p3[0],p3[1],Z+dimZ);
		}
	}
}


void disegnaMuroPrincipale(){
	int i;
		glEnable( GL_POLYGON_OFFSET_FILL );
		glPolygonOffset(1,1);
				applyTexture(TX_WALL_S1);
				glPushMatrix();
					glBegin(GL_QUADS);
					for (i=0;i<NUMBER_OF_WALLS_S1;i++)
					{
						drawWall(&walls1[i]);
					}
					glEnd();
				glPopMatrix();
				applyTexture(TX_WALL_S2);
				glPushMatrix();
					glBegin(GL_QUADS);
					for (i=0;i<NUMBER_OF_WALLS_S2;i++)
					{
						drawWall(&walls2[i]);
					}
					glEnd();
				glPopMatrix();
				applyTexture(TX_WALL_S3);
				glPushMatrix();
					glBegin(GL_QUADS);
					for (i=0;i<NUMBER_OF_WALLS_S3;i++)
					{
						drawWall(&walls3[i]);
					}
					glEnd();
				glPopMatrix();
				applyTexture(TX_WALL_S4);
				glPushMatrix();
					glBegin(GL_QUADS);
					for (i=0;i<NUMBER_OF_WALLS_S4;i++)
					{
						drawWall(&walls4[i]);
					}
					glEnd();
				glPopMatrix();
				applyTexture(TX_WALL_S5);
				glPushMatrix();
					glBegin(GL_QUADS);
					for (i=0;i<NUMBER_OF_WALLS_S5;i++)
					{
						drawWall(&walls5[i]);
					}
					glEnd();
				glPopMatrix();
		
				applyTexture(TX_WALL_S1);
				glPushMatrix();
					glBegin(GL_QUADS);
					for (i=0;i<NUMBER_OF_WALLS_C1;i++)
					{
						drawWall(&wallsc1[i]);
					}
					glEnd();
				glPopMatrix();
				applyTexture(TX_WALL_S1);
				glPushMatrix();
					glBegin(GL_QUADS);
					for (i=0;i<NUMBER_OF_WALLS_C2;i++)
					{
						drawWall(&wallsc2[i]);
					}
					glEnd();
				glPopMatrix();
				applyTexture(TX_WALL_S1);
				glPushMatrix();
					glBegin(GL_QUADS);
					for (i=0;i<NUMBER_OF_WALLS_C3;i++)
					{
						drawWall(&wallsc3[i]);
					}
					glEnd();
				glPopMatrix();
				applyTexture(TX_WALL_S1);
				glPushMatrix();
					glBegin(GL_QUADS);
					for (i=0;i<NUMBER_OF_WALLS_C4;i++)
					{
						drawWall(&wallsc4[i]);
					}
					glEnd();
				glPopMatrix();
		glDisable(GL_POLYGON_OFFSET_FILL);
}




void disegnaMappa(
	GLfloat eyex, GLfloat eyey,	GLfloat eyez, 
	GLfloat targetx, GLfloat targety, GLfloat targetz 
){
		int i;
		glPushMatrix();
				glTranslatef(140,-80,30);
				glPushMatrix();
					glTranslatef(eyex,eyey,eyez-20);
						glutSolidSphere(1.5, 10, 10);
				glPopMatrix();
				
				glPushMatrix();
					glTranslatef(0,0,eyez-20);
					glBegin(GL_LINES);
						glVertex3f(eyex,eyey,eyez);
						glVertex3f(targetx,targety,targetz);
					glEnd();
				glPopMatrix();
				
				glBegin(GL_QUADS);
					for (i=0;i<NUMBER_OF_WALLS;i++)
					{
						drawWall(&walls[i]);
					}
				glEnd();
		glPopMatrix();
}

int isInRoom(room_t* roomMap,float eye_x, float eye_y){
	int i = 0;
	GLfloat x1;
	GLfloat y1;
	GLfloat x2;
	GLfloat y2;
//		printf("**********************\n");
	for (i = 1; i<=NUMBER_OF_ROOM; i++){
		x1 = roomMap[i].x1;
		y1 = roomMap[i].y1;
		x2 = roomMap[i].x2;
		y2 = roomMap[i].y2;
		if ((eye_x >= x1) 
			&& (eye_y <= y1) 
			&& (eye_x <= x2) 
			&& (eye_y >= y2)
			)
		{
//			printf("L'osservatore e' all'interno della STANZA %d \n",i);
			return i;
		}
	}
//		printf("**********************\n");
	return -1;
}


void defineRoom(room_t* roomstmp,
	GLfloat x1, // coordinate che definiscono il perimetro attivo della square
	GLfloat y1,
	GLfloat x2,
	GLfloat y2
		){
		roomstmp->x1 = x1;
		roomstmp->y1 = y1;
		roomstmp->x2 = x2;
		roomstmp->y2 = y2;
}

room_t* initRooms (){
	room_t *roomMap;
	GLfloat x1,y1,x2,y2;
	roomMap = (void*)malloc(sizeof(room_t) * (NUMBER_OF_ROOM+1));

	x1 = stanza1_start[0];y1 = stanza1_start[1];
	x2 = stanza1_end[0];y2 = stanza1_end[1];
	defineRoom(
		&roomMap[STANZA1],
		x1,y1,x2,y2 
	);
	
	x1 = stanza2_start[0];y1 = stanza2_start[1];
	x2 = stanza2_end[0];y2 = stanza2_end[1];
	defineRoom(
		&roomMap[STANZA2],
		x1,y1,x2,y2 
	);

	x1 = stanza3_start[0];y1 = stanza3_start[1];
	x2 = stanza3_end[0];y2 = stanza3_end[1];
	defineRoom(
		&roomMap[STANZA3],
		x1,y1,x2,y2 
	);
	
	x1 = stanza4_start[0];y1 = stanza4_start[1];
	x2 = stanza4_end[0];y2 = stanza4_end[1];
	defineRoom(
		&roomMap[STANZA4],
		x1,y1,x2,y2 
	);

	x1 = stanza5_start[0];y1 = stanza5_start[1];
	x2 = stanza5_end[0];y2 = stanza5_end[1];
	defineRoom(
		&roomMap[STANZA5],
		x1,y1,x2,y2
	);

	x1 = corridoio11_start[0];y1 = corridoio11_start[1];
	x2 = corridoio11_end[0];y2 = corridoio11_end[1];
	defineRoom(
		&roomMap[CORRIDOIO11],
		x1,y1,x2,y2 
	);
	
	x1 = corridoio12_start[0];y1 = corridoio12_start[1];
	x2 = corridoio12_end[0];y2 = corridoio12_end[1];
	defineRoom(
		&roomMap[CORRIDOIO12],
		x1,y1,x2,y2 
	);

	x1 = corridoio13_start[0];y1 = corridoio13_start[1];
	x2 = corridoio13_end[0];y2 = corridoio13_end[1];
	defineRoom(
		&roomMap[CORRIDOIO13],
		x1,y1,x2,y2 
	);
	
	x1 = corridoio14_start[0];y1 = corridoio14_start[1];
	x2 = corridoio14_end[0];y2 = corridoio14_end[1];
	defineRoom(
		&roomMap[CORRIDOIO14],
		x1,y1,x2,y2 
	);

	
	return roomMap;
}
